package cn.sczhyt.modules.sys.service.impl;

import cn.sczhyt.modules.sys.form.OrganizeInfoForm;
import cn.sczhyt.modules.sys.form.UserForm;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.Query;
import cn.sczhyt.modules.sys.dao.SysOrganizeInfoDao;
import cn.sczhyt.modules.sys.dao.SysUserDao;
import cn.sczhyt.modules.sys.dao.SysUserRoleDao;
import cn.sczhyt.modules.sys.entity.SysOrganizeInfoEntity;
import cn.sczhyt.modules.sys.entity.SysUserEntity;
import cn.sczhyt.modules.sys.entity.SysUserRoleEntity;
import cn.sczhyt.modules.sys.service.SysOrganizeInfoService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.sczhyt.common.utils.ShiroUtils.getUserId;


@Service("sysOrganizeInfoService")
public class SysOrganizeInfoServiceImpl extends ServiceImpl<SysOrganizeInfoDao, SysOrganizeInfoEntity> implements SysOrganizeInfoService {


    @Autowired
    private SysUserDao userDao;

    @Autowired
    private SysOrganizeInfoDao organizeInfoDao;

    @Autowired
    private SysUserRoleDao userRoleDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysOrganizeInfoEntity> page = this.page(
                new Query<SysOrganizeInfoEntity>().getPage(params),
                new QueryWrapper<SysOrganizeInfoEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void AddOrg(OrganizeInfoForm vo, String roleId) {
        // 获取userEntity
        System.out.println(vo);
        UserForm form = vo.getUserEntity();
        String email = form.getEmail();
        String mobile = form.getMobile();
        //获取前端新增的信息,设置账户密码，加盐，
        String password = form.getPassword();
        String username = form.getUsername();
        SysUserEntity userEntity = new SysUserEntity();

        //生成随机盐
        String salt = RandomStringUtils.randomAlphanumeric(20);
        userEntity.setSalt(salt); //保存盐
        userEntity.setPassword(new Sha256Hash(password,salt).toHex());

        //默认账户可用
        userEntity.setStatus(1);
        userEntity.setCreateUserId(getUserId());
        userEntity.setCreateTime(new Date());
        userEntity.setUsername(username);
        userEntity.setMobile(mobile);
        userEntity.setEmail(email);

        //存储账号和密码、存储user
        userDao.insert(userEntity);
        //
        System.out.println(userEntity);
        String userId = userEntity.getUserId();
        System.out.println(userId);
        //保存机构详细信息
        SysOrganizeInfoEntity organizeInfoEntity = vo.getOrganizeInfoEntity();
        System.out.println(organizeInfoEntity);
        organizeInfoEntity.setUserId(userId);
        organizeInfoDao.insert(organizeInfoEntity);

        //保存用户机构角色
        SysUserRoleEntity userRole = new SysUserRoleEntity();
        userRole.setUserId(userEntity.getUserId());
        userRole.setRoleId(roleId);
        userRoleDao.insert(userRole);

    }

    @Override
    public List<SysOrganizeInfoEntity> findAll() {
        return organizeInfoDao.findAll();
    }

    @Override
    public List<SysOrganizeInfoEntity> findByIds(String[] ids) {
        return organizeInfoDao.findByIds();
    }

    @Override
    public List<SysOrganizeInfoEntity> findAllDw() {
        return organizeInfoDao.findAllDw();
    }

    @Override
    public SysOrganizeInfoEntity getByUserId(String userId) {

        return organizeInfoDao.selectByUserId(userId);
    }
}
